package cn.oms.manage.dao.persist.repository.impl;

import cn.oms.commons.util.StringToDate;
import cn.oms.manage.dao.persist.mapper.OrderMapper;
import cn.oms.manage.dao.persist.repository.IOrderRepositry;
import cn.oms.manage.pojo.entity.Order;
import cn.oms.manage.pojo.param.OrderUpdateParam;
import cn.oms.manage.pojo.vo.OrderListVo;
import cn.oms.manage.pojo.vo.OrderStandardVO;
import cn.oms.manage.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class OrderRepositry implements IOrderRepositry {
    @Autowired
    OrderMapper orderMapper;
    @Override
    public int insert(Order order) {
        log.info("开始添加预约订单");
        return orderMapper.insert(order);
    }

    @Override
    public int deleteById(Long id) {
        log.info("开始根据Id删除订单！");
        return orderMapper.deleteById(id);
    }

    @Override
    public int updateById(Long id,OrderUpdateParam orderUpdateParam) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        Order order = new Order();
        order.setAppointment(StringToDate.StrToDate(orderUpdateParam.getAppointment()));
        BeanUtils.copyProperties(orderUpdateParam,order);
        return orderMapper.update(order,wrapper);
    }

    @Override
    public int updateState(Long id) {
        Order order = new Order();
        order.setState("0");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        return orderMapper.update(order,wrapper);
    }

    @Override
    public List<OrderListVo> list() {
        log.info("开始查询所有未过期订单");
        return orderMapper.list();
    }

    @Override
    public List<OrderListVo> listByUsername(String username) {
        return orderMapper.listByUsername(username);
    }

    @Override
    public List<OrderListVo> listByDoctor(String doctorName) {
        return orderMapper.listByDoctor(doctorName);
    }

    @Override
    public List<OrderListVo> listByState(String state) {
        return orderMapper.listByState(state);
    }

    @Override
    public OrderStandardVO getStandardById(Long id) {
        return orderMapper.getStandardById(id);
    }

    @Override
    public Long getCustomerId(String phone, String username) {
        return orderMapper.getCustomerIdByPhoneAndUsername(phone,username);
    }

    @Override
    public int countByCustomer(Long CustomerId) {
        log.debug("开始执行【统计匹配客户的订单的数量】的数据访问，id：{}", CustomerId);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", CustomerId);
        return orderMapper.selectCount(queryWrapper);
    }
}
